#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include <time.h>
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include "os_api.h"
#include "debug.h"
#include "adc.h"
#include "dev_api.h"


#define OEM_APPVER	"160X_Vxx.xx [ "__DATE__ __TIME__" ]"

#define _STACK_SIZE		(1024*6)	

static void* _task_stack = NULL;

static OSTaskRef _task_ref = NULL;

OSATimerRef _task_delete_timer_ref = NULL;


void DeleteTaskByTimer(UINT32 id)
{
    OSA_STATUS status;

    if(_task_ref)
    {
        status = OSATaskDelete(_task_ref);
        ASSERT(status == OS_SUCCESS);
        _task_ref = NULL;
	   
        if(_task_stack)
        {
            free(_task_stack);
            _task_stack = NULL;
        }
    }

    OSATimerDelete (_task_delete_timer_ref);
}


static void _task(void *ptr)
{
	int count = 10;	
	UINT16 adcValue = 0;
	unsigned int current_os_ticks;	

	// hal_AdcConfig(1);
	
	while(count--){
		
		current_os_ticks = OSAGetTicks();	// 获取系统tick ,一个tick 5ms，即开机时间	
		LOG_PRINTF("this is a test task current_os_ticks %d\n", current_os_ticks);

		adcValue = hal_AdcRead(0);
		LOG_PRINTF("hal_AdcRead 0 adcValue %d\n", adcValue);
		
		adcValue = hal_AdcRead(1);
		LOG_PRINTF("hal_AdcRead 1 adcValue %d\n", adcValue);	
		
		// 1602模块支持直接读取电源电压		
		adcValue = hal_AdcVbatRead();
		LOG_PRINTF("hal_AdcVbatRead VbatValue %d\n", adcValue);	

		OSATaskSleep(200);					// 如果可能存在while空跑的case，一定需要加上个sleep，不然可能会造成系统调度异常
	}
	
	OSATimerCreate(&_task_delete_timer_ref);
	OSATimerStart(_task_delete_timer_ref, 400, 0, DeleteTaskByTimer, 0);
}


int main(void)
{
    int ret;
	
	dev_SetAppVersion(OEM_APPVER);

	_task_stack = malloc(_STACK_SIZE);
	ASSERT(_task_stack != NULL);
	
    ret = OSATaskCreate(&_task_ref, _task_stack, _STACK_SIZE, 100, "_task", _task, NULL);
    ASSERT(ret == OS_SUCCESS);

	return 0;
}